package com.captain.special.链表;

import com.captain.leetcode.链表.ListNode;

import java.util.HashSet;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/30 8:52
 */
public class 链表组件817 {
    public static void main(String[] args) {
        new 链表组件817().numComponents(ListNode.getInstance(), new int[]{4});
        //[3,4,0,2,1]
        //[4]

    }

    public int numComponents(ListNode head, int[] nums) {
        if (head == null) return 0;
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int count = 0;
        //是否能成为组件标识
        boolean flag = false;
        while (head != null) {
            int curValue = head.val;
            //当前值是否包含在数据中, 如果不包含,则移动指针.如果有,则向下移动
            if (set.contains(curValue)) {
                head = head.next;
                //解决尾边界问题
                if (head == null) {
                    count++;
                }
                flag = true;
                continue;
            }
            //如果不包含,并且存在已经包含的
            if (flag) {
                count++;
                flag = false;
            }
            head = head.next;
        }
        return count;
    }
}
